import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from "@/store"

const originalPush = VueRouter.prototype.push;

VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/home"
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
  {
    path: '/goods',
    name: 'goods',
    component: () => import(/* webpackChunkName: "goods" */ '../views/GoodsView.vue')
  },
  {
    path: '/user',
    name: 'user',
    redirect:"/user/cart",
    component: () => import(/* webpackChunkName: "user" */ '../views/UserView.vue'),
    children:[
      {
        path: '/user/cart',
        name: 'cart',
        component: () => import(/* webpackChunkName: "cart" */ '../components/user/Cart.vue')
      },
    ]
  },
  {
    path: '/order',
    name: 'order',
    component: () => import(/* webpackChunkName: "order" */ '../views/OrderView.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import(/* webpackChunkName: "free" */ '../views/FreeView.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import(/* webpackChunkName: "details" */ '../views/DetailsView.vue')
  },
  {
    path:"*",
    name: 'error',
    component: () => import(/* webpackChunkName: "error" */ '../views/ErrorView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})



// 全局路由守卫 
// router.beforeEach((to, from, next)=>{
// //   // 什么执行这个函数？   路由跳转之前就会执行这里的代码
// //   console.log("to为: ", to);  // 要去到的路由对象
// //   console.log("from为: ", from);  //从哪个路由出发的路由对象


// //   // 需求: 在进入到/user之前, 判断有没有token   有就放行, 没有就提示请先登录, 不放行
//   if(to.path=="/user"){
//     let token = localStorage.getItem("x-auth-token");
//     if(token){
//       // 有就放行
//       next()
//     }else{
//       // 提示
//       store.dispatch("toastStatus/asyncChanIsShowToast",{
//         msg:"请先登录!",
//         type:"warning"
//       });

//     }

//     return // 需要写return  防止上面的next()执行完,还继续执行下面的next()
//   }

//   next()  // 放行
// })


export default router
